<template>
  <div class="add_comment">
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">评价内容：</el-col>
      <el-col :span="20">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="currEditInfo.content"
        />
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">上传图片：</el-col>
      <el-col :span="20">
        <el-upload
          action="http://47.103.2.155:8801/api/File/upload"
          list-type="picture-card"
          name="file"
          :on-success="(obj,file,list) => onUpload(obj,file,list,idx)"
          :limit="6"
          :file-list="imageList"
          :class="{ hide: hideUploadBtn }"
          :on-remove="onRemoveImageList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-col>
    </el-row>
    <el-row :span="24" style="display: flex;justify-content: center;">
      <el-col :span="6" style="text-align: center;">
        <el-button type="primary" @click="onSave">保存</el-button>
        <el-button type="primary" plain @click="onClose">关闭</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { createYzComment } from '@/api/yzComment';

export default {
  name: 'addComment',
  props: {
    orderInfo: {}
  },
  data() {
    return {
      imageList: [],
      currEditInfo: {},
      hideUploadBtn: false
    };
  },
  methods: {
    onUpload(obj,res,file,idx){
        if (obj.code == 200) {
          this.$message.success({dangerouslyUseHTMLString: true, message: obj.msg})
          this.handleImageList(file)
        } else {
          this.$message.error('操作失败')
        }
      },
      handleImageList(fileList) {
        this.currEditInfo.attements = '';
        this.imageList = fileList.map((item, index) => {
          const url = (item['response'] && item['response']['data'] && item['response']['data']['fileUrl']) || item.url;
          this.currEditInfo.attements += url + (index === fileList.length - 1 ? '' : ',')
          return { name: item.name, url }
        })
        this.hideUploadBtn = this.imageList.length >= 6;
      },
      onRemoveImageList(file, fileList) {
        this.handleImageList(fileList);
      },
      async onSave() {
        const userInfo = sessionStorage.getItem('userInfo');
        const userId = JSON.parse(userInfo)['id'];
        const params = {
          ...this.currEditInfo,
          orderId: this.orderInfo.id, ouserId: userId
        }
        const { data } = await createYzComment(params);
        if (data.code === 200) {
          this.$message({
            message: data.msg || '保存成功',
            type: 'success',
          });
          this.$emit('onCloseToggle')
        } else {
          this.$message({
            message: data.msg || '保存失败',
            type: 'error',
            showClose: true,
            duration: 3000,
          });
        }
      },
      onClose() {
        this.$emit('onCloseToggle')
      }
  }
}
</script>
<style scoped>

</style>